<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="./libs/react.js"></script>
    <script src="./libs/react-dom.js"></script>
    <script src="./libs/babel.min.js"></script>
    <script type="text/babel">
      // 跨组件之间传参，在各个组件之间进行传递
      const mainContext = React.createContext();
      console.log(mainContext);
      const { Provider } = mainContext;

      function B() {
        const { counter } = React.useContext(mainContext);
        return (
          <>
            <p onClick={() => counter.setCount(counter.count + 1)}>
              我是一个组件B
            </p>
          </>
        );
      }

      function App(props) {
        const { counter } = React.useContext(mainContext);
        return (
          <>
            <h1>当前的count值为：{counter.count}</h1>
            <B />
          </>
        );
      }
      function AppProvider(props) {
        const [count, setCount] = React.useState(0);
        return (
          <Provider value={{ counter: { count, setCount } }}>
            {props.children}
          </Provider>
        );
      }

      ReactDOM.render(
        <AppProvider>
          <App />
        </AppProvider>,
        document.getElementById("app")
      );
    </script>
  </body>
</html>
